SQL Server 2017 brings several improvements to us. Sometimes a simple new field in a DMV can turn possible interesting new functionalities. That’s what happens with differential backup in SQL Server 2017.
The DMV sys.dm_db_file_space_usage has a new field: modified_extent_page_count . This new field tell us how many pages were changed since the last full backup. Basically, it’s the information kept by DCM (Differential Change Map) that until now was very difficult to check.. We could use DBCC Page for this, however it’s not possible to use the information in automated scripts.
What are the possibilities with this new field ? We are now able to check how many extents have changed since last full backup and decide if a full backup is really needed or we can live with a differential backup, achieving smarter backup plans.
Change our full backup jobs to first check this field and decide if the backup will be full or differential can save space and maintenance time with databases that aren’t updated so often.
Let’s do a simple walkthrough to illustrate this. I will use AdventureWorks2016 database for this demonstration, you can download this sample database here: https://www.microsoft.com/en-us/download/details.aspx?id=49502
-
Change the recovery model and take the first full backup:
1234567use AdventureWorks2016CTP3goAlter database AdventureWorks2016CTP3 set recovery fullgo— The initial full backupbackup database adventureworks2016ctp3to disk=‘c:\backups\newCopy.bak’ -
Check the modified pages
1234— Identifying the amount of changesSelect file_id,total_page_count, modified_extent_page_count,(100 * modified_extent_page_count)/total_page_count [percent]from sys.dm_db_file_space_usage -
Let’s do a lot of updates:
123456789101112131415161718— Let’s do a lot of updatesupdate sales.SalesOrderDetailset orderqty=orderqty+1update sales.ordertrackingset eventdetails=eventdetails + ‘x’update production.TransactionHistoryset quantity=quantity +1update production.TransactionHistoryArchiveset quantity=quantity +1update sales.SalesOrderDetail_ondiskset OrderQty= OrderQty +1update person.personset emailpromotion=1 -
Let’s check the modified pages again and run a simple backup script. There are not enough modified pages, the script will choose a differential backup.
123456— Identifying the amount of changes – againSELECT file_id,total_page_count,modified_extent_page_count,( 100 * modified_extent_page_count ) / total_page_count [percent]FROM sys.dm_db_file_space_usage1234567891011121314151617— Our backup script will be like thisDECLARE @changes NUMERIC(15, 3)SELECT @changes = ( 100 * Sum(modified_extent_page_count) /Sum(total_page_count) )FROM sys.dm_db_file_space_usageIF @changes > 65BEGIN— Too many changes, do a full backupBACKUP DATABASE adventureworks2016ctp3 TO DISK=‘c:\backups\newCopy.bak’ENDELSEBEGIN— Too few changes, do a differential backupBACKUP DATABASE adventureworks2016ctp3 TO DISK=‘c:\backups\newCopy.bak’WITHdifferentialEND -
A lot more updates
12345678910111213— Let’s do a lot more updatesUPDATE sales.customerpiiSET lastname = lastname + ‘x’UPDATE production.workorderroutingSET actualcost = actualcost + 1UPDATE sales.salesorderheaderSET subtotal = subtotal + 1UPDATE sales.salesorder_jsonSET subtotal = subtotal + 1UPDATE person.addressSET addressline1 = addressline1 + ‘x’UPDATE person.emailaddressSET emailaddress = emailaddress + ‘x’ -
Again, check the modified pages and our backup script. Now there are a lot of modified extents, the script will choose a full backup.
123456— Identifying the amount of changes – againSELECT file_id,total_page_count,modified_extent_page_count,( 100 * modified_extent_page_count ) / total_page_count [percent]FROM sys.dm_db_file_space_usage12345678910111213141516— Our backup script will be like thisDECLARE @changes NUMERIC(15, 3)SELECT @changes = ( 100 * Sum(modified_extent_page_count) /Sum(total_page_count) )FROM sys.dm_db_file_space_usageIF @changes > 65BEGIN— Too many changes, do a full backupBACKUP DATABASE adventureworks2016ctp3 TO DISK=‘c:\backups\newCopy.bak’ENDELSEBEGIN— Too few changes, do a differential backupBACKUP DATABASE adventureworks2016ctp3 TO DISK=‘c:\backups\newCopy.bak’WITH differentialEND -
Check the modified pages again. The full backup cleaned the DCM.
123456— Identifying the amount of changes – againSELECT file_id,total_page_count,modified_extent_page_count,( 100 * modified_extent_page_count ) / total_page_count [percent]FROM sys.dm_db_file_space_usage
Load comments